#!/usr/bin/env node
/**
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the license found in the LICENSE file in
 * the root directory of this source tree.
 *
 * @noflow
 * @format
 */
'use strict';

/* eslint nuclide-internal/no-commonjs: 0 */

const fs = require('fs');
const path = require('path');

const basedir = path.join(__dirname, '..');

const gitIgnorePath = path.join(basedir, '.gitignore');
const npmIgnorePath = path.join(basedir, '.npmignore');
const packageJsonPath = path.join(basedir, 'package.json');

const gitIgnore = fs.readFileSync(gitIgnorePath, 'utf8');
const npmIgnore = fs.readFileSync(npmIgnorePath, 'utf8');
const packageJson = fs.readFileSync(packageJsonPath, 'utf8');

const pkg = JSON.parse(packageJson);

/**
 * .gitignore:
 */
const gitIgnoreLines = gitIgnore.split('\n');
gitIgnoreLines.push(
  '',
  '########################################################',
  '### !!!AUTO GENERATED by "prepare_apm_release.js"!!! ###',
  '########################################################',
  '',
  '# "apm" doesn\'t honor ".npmignore" files. As a workaround, we merge the',
  '# ".npmignore" content into ".gitignore":',
  npmIgnore,
  '',
);
fs.writeFileSync(gitIgnorePath, gitIgnoreLines.join('\n'));

/**
 * package.json:
 */
const pkgCopy = JSON.parse(JSON.stringify(pkg));
delete pkgCopy.private;
const newPackageJson = JSON.stringify(pkgCopy, null, 2) + '\n';
fs.writeFileSync(packageJsonPath, newPackageJson);
